/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.swing;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.BorderFactory;
import javax.swing.JComponent;

import org.jdesktop.swingx.JXCollapsiblePane;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.JXTitledPanel;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class CompositePanel extends JXPanel
{
    private static final long serialVersionUID = 5541503007680664816L;
    private final Map<String, JXCollapsiblePane> panesMap;
    
    private FormLayout layout;
    
    public CompositePanel()
    {
        super();
        
        this.panesMap = new HashMap<String, JXCollapsiblePane>(); 
        init();
    }
    
    private void init()
    {
        this.layout = new FormLayout("fill:100dlu:grow");
        setLayout(layout);
    }
    
    private void expand(String tag)
    {
        for (Entry<String, JXCollapsiblePane> entry : this.panesMap.entrySet())
        {
            entry.getValue().setCollapsed(!entry.getKey().equals(tag));
        }
    }

    public void addComponent(JComponent component, final String tag)
    {
        if (tag == null || tag.trim().equals(""))
        {
            throw new IllegalArgumentException("Null or empty tag is not allowed");
        }

        final JXCollapsiblePane colPane = new JXCollapsiblePane();
        colPane.getContentPane().setLayout(new BorderLayout());
        colPane.getContentPane().add(component, BorderLayout.CENTER);
        colPane.setAnimated(false);

        JXTitledPanel titledPanel = new JXTitledPanel(tag, colPane);
        titledPanel.setBorder(BorderFactory.createEmptyBorder());
        titledPanel.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                if (e.getButton() == MouseEvent.BUTTON1)
                {
                    System.out.println("click");
                    expand(tag);
                }
            }
        });
        CellConstraints cc = new CellConstraints();
        this.layout.appendRow(RowSpec.decode("fill:100px:grow"));
        add(titledPanel, cc.xy(1, this.layout.getRowCount()));

        this.panesMap.put(tag, colPane);
        expand(tag);
    }

}